<?php

namespace App\Admin\Controllers\portal;

use App\Models\SwtArticle;
use App\Models\SwtPortalConfig;
use Dcat\Admin\Form;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;


class SwtPortalConfigController extends AdminController
{

    public function index(Content $content)
    {

        return $content
            ->translation($this->translation())
            ->title($this->title)
            ->body($this->form());
    }

    protected function form()
    {
        return Form::make(new SwtPortalConfig(), function (Form $form) {

            $configs = SwtPortalConfig::get();

            $default = [];

            $content = ['introduction','founder','bylaws'];

            foreach($configs as $v) {
                if(in_array($v['key'], $content)) {
                    $default[$v['key']] = $v['content'];
                } else {
                    $default[$v['key']] = $v['value'];
                }
            }

            $form->action('/portal/config');

            $form->title('配置');
            $form->disableDeleteButton();
            $form->disableListButton();
            $form->disableViewButton();
            $form->disableResetButton();

            $form->tab('首页配置', function (Form $form) use ($default) {
                $form->multipleImage('event_photos')
                    ->url('/upload')
                    ->uniqueName()
                    ->autoUpload()
                    ->maxSize(5120)
                    ->default($default['event_photos'] ?? '')
                    ->limit(2)
                    ->required();

                $form->multipleImage('hotspot_event_photos')
                    ->url('/upload')
                    ->uniqueName()
                    ->autoUpload()
                    ->maxSize(5120)
                    ->default($default['hotspot_event_photos'] ?? '')
                    ->limit(2)
                    ->required();

                $form->multipleImage('carousel_photos')
                    ->url('/upload')
                    ->uniqueName()
                    ->autoUpload()
                    ->maxSize(5120)
                    ->default($default['carousel_photos'] ?? '')
                    ->required();

                $form->multipleImage('pledge_photos')
                    ->url('/upload')
                    ->uniqueName()
                    ->autoUpload()
                    ->maxSize(5120)
                    ->default($default['pledge_photos'] ?? '')
                    ->required();
            });

            $form->tab('捐款配置', function (Form $form) use ($default) {
                $form->text('account_name') -> required() -> default($default['account_name'] ?? '');
                $form->text('bank_name') -> required() -> default($default['bank_name'] ?? '');
                $form->text('card_no') -> required() -> default($default['card_no'] ?? '');
                $form->text('remark') -> required() -> default($default['remark'] ?? '');
                $form->multipleImage('pay_code')
                    ->url('/upload')
                    ->uniqueName()
                    ->autoUpload()
                    ->maxSize(5120)
                    ->default($default['pay_code'] ?? '')
                    ->required();
            });

            $form->tab('基金会简介', function (Form $form) use ($default) {
                $form->editor('introduction')->required()->default($default['introduction'] ?? '');
            });

            $form->tab('创始人寄语', function (Form $form) use ($default) {
                $form->image('founder_bg')
                    ->url('/upload')
                    ->uniqueName()
                    ->autoUpload()
                    ->maxSize(5120)
                    ->default($default['founder_bg'] ?? '')
                    ->required();
                $form->editor('founder') -> required() -> default($default['founder'] ?? '');
            });

            $form->tab('章程', function (Form $form) use ($default) {
                $form->editor('bylaws') -> required() -> default($default['bylaws'] ?? '');
            });

            $form->tab('组织架构图', function (Form $form) use ($default) {
                $form->image('user_org')
                    ->url('/upload')
                    ->uniqueName()
                    ->autoUpload()
                    ->maxSize(5120)
                    ->default($default['user_org'] ?? '')
                    ->required();
            });

            $form->tab('基金会成员图', function (Form $form) use ($default) {
                $form->image('user')
                    ->url('/upload')
                    ->uniqueName()
                    ->autoUpload()
                    ->default($default['user'] ?? '')
                    ->required();
            });

            $form->saving( function (Form $form) use($content) {

                $form->deleteInput('_token');
                $form->deleteInput('file');
                $form->deleteInput('_file_');
                $form->deleteInput('_previous_');

                foreach($form -> input() as $key => $v)
                {
                    $data = [
                        'name' => trans('swt-portal-config.fields.'.$key),
                        'key'  => $key
                    ];

                    if(in_array($key, $content)) {
                        $data['content'] = $v;
                    } else {
                        $data['value'] = $v;
                    }

                    SwtPortalConfig::updateOrCreate(
                        ['key' => $key],
                        $data
                    );
                }

                return $form->response()->success('保存成功');
            });
        });
    }
}
